﻿// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Copyright (c) 2011, Dr. Masroor Ehsan
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the author nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
//  $Author: masroore@gmail.com $
//  $Id: IntradayStockSnapshot.cs 16 2011-10-13 09:22:56Z masroore@gmail.com $
//  $Rev: 14 $
// 
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

using System;
using System.Collections.Generic;
using System.Linq;

namespace MaxTrader.IntradayQuotes.Core
{
    public class IntradayStockSnapshot : ISnapshot
    {
        private readonly SortedList<DateTime, IntradayQuote> _quotesHistory;

        public IntradayStockSnapshot(string symbol, StockExchange exchange)
        {
            Symbol = symbol;
            Exchange = exchange;
            _quotesHistory = new SortedList<DateTime, IntradayQuote>();
        }

        public StockExchange Exchange { get; private set; }

        public string Symbol { get; private set; }

        public decimal High { get; internal set; }

        public decimal Low { get; internal set; }

        public IntradayQuote LastQuote()
        {
            return (_quotesHistory.Count > 0) ? _quotesHistory.Last().Value : null;
        }

#if TRACK_TICK_SEQUENCE
        public void Add(DateTime utcTime, decimal lastPrice, int cumVol, int volDiff = 0, int seqNum = 0)
#else
        public void Add(DateTime utcTime, decimal lastPrice, int cumVol, int volDiff = 0)
#endif
        {
            // require = volume & price > 0, utcTime > datemin
            var tick = new IntradayQuote(this)
                           {
#if TRACK_TICK_SEQUENCE
                               SequenceNumber = seqNum,
#endif
                               CumulativeVolume = cumVol,
                               Price = lastPrice,
                               UtcTimeStamp = utcTime,
                               VolumeDifference = volDiff
                           };

            // If this is the first captured tick, set 
            // the initial volume difference to current volume
            if ((_quotesHistory.Count == 0) && (volDiff == 0))
                tick.VolumeDifference = cumVol;

            if (lastPrice > High)
            {
                High = lastPrice;
            }
            else if (lastPrice < Low)
            {
                Low = lastPrice;
            }

            _quotesHistory.Add(utcTime, tick);
        }

        public IEnumerable<IIntradayQuote> GetQuotes()
        {
            return _quotesHistory.Values.ToList();
        }

        public void Clear()
        {
            _quotesHistory.Clear();
        }
    }
}